package com.stu.covid.business.service;

import com.stu.covid.business.entity.Teacher;
import com.stu.covid.business.entity.User;
import com.stu.covid.business.manager.SystemManager;
import com.stu.covid.business.mapper.TeacherMapper;
import com.stu.covid.business.utils.CodeMsg;
import com.stu.covid.business.utils.PasswordUtils;
import com.stu.covid.business.utils.Result;
import com.stu.covid.business.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private SystemManager systemManager;

    public Result<Teacher> teacherList(Teacher teacher){
        List<Teacher> list = teacherMapper.list(teacher);
        Integer count = teacherMapper.count(teacher);
        return ResultFactory.bulid(CodeMsg.SUCCESS,count,list);
    }

    public void teacherAdd(Teacher teacher) {

        String workNo = systemManager.getIncreaseNumber("A", "student", 1, 4);

        User user = new User();
        user.setRole("teacher");
        user.setUsername(workNo);
        user.setPassword(PasswordUtils.genPassword("123456"));
        user.setNickName(teacher.getName());
        user.setStatus(1);
        user.setIsDelete(0);

        teacher.setUserId(user.getId());
        teacher.setWorkNo(workNo);
        teacherMapper.insertSelective(teacher);
    }

    public void teacherUpdate(Teacher teacher) {
        teacherMapper.updateByPrimaryKeySelective(teacher);
    }

    public void teacherDel(Integer id) {
        teacherMapper.deleteByPrimaryKey(id);
    }
}
